import Vue from 'vue'
import Router from 'vue-router'
import store from './../store'
import { setTitle } from '@/config/utils'
import { getStore } from '@/config/utils'
/**默认页面 */
const Page = r => require.ensure([], () => r(require('@layout/page.vue')), 'page')
/** 登录页 */
const Login = r => require.ensure([], () => r(require('@layout/login.vue')), 'login')
/** 我的门户 */
const Home = r => require.ensure([], () => r(require('@views/Home/Home.vue')), 'Home')
/** 首页 */
const homePage = r => require.ensure([], () => r(require('@views/Home/homePage/homePage.vue')), 'homePage')
// 通知列表
const noticeIndex =  r => require.ensure([], () => r(require('@views/Home/sendnotice/index.vue')), 'index');
// 发通知
const sendnotice = r => require.ensure([], () => r(require('@views/Home/sendnotice/Sendnotice.vue')), 'sendnotice');
// 校内通知
const notificationlist = r => require.ensure([], () => r(require('@views/Home/sendnotice/Notificationlist.vue')), 'notificationlist');
// 通知正文
const content = r => require.ensure([], () => r(require('@views/Home/sendnotice/Content.vue')), 'content');
/** 我的收藏 */
const myCollection = r => require.ensure([], () => r(require('@views/Home/myCollection/myCollection.vue')), 'myCollection')
/** 系统应用 */
const sysTemApplication = r => require.ensure([], () => r(require('@views/Home/sysTemApplication/sysTemApplication.vue')), 'sysTemApplication')
/** 校历日历 */
const schoolCalendar = r => require.ensure([], () => r(require('@views/Home/schoolCalendar/schoolCalendar.vue')), 'schoolCalendar')
/** 校内网站 */
const xiaoNeiNet = r => require.ensure([], () => r(require('@views/Home/xiaoNeiNet/xiaoNeiNet.vue')), 'xiaoNeiNet')
/** 服务中心 */
const serviceCenter = r => require.ensure([], () => r(require('@views/serviceCenter/serviceCenter.vue')), 'serviceCenter')
// 我要反馈
const sendfeedback = r => require.ensure([], () => r(require('@views/Home/myfeedback/Sendfeedback.vue')), 'sendfeedback');
// 反馈列表
const feedbacklist = r => require.ensure([], () => r(require('@views/Home/myfeedback/Feedbacklist.vue')), 'feedbacklist');
// 反馈内容
const feedbackcontent = r => require.ensure([], () => r(require('@views/Home/myfeedback/Content.vue')), 'feedbackcontent');
// 办公电话
const officephone = r => require.ensure([], () => r(require('@views/Home/officephone/Officephone.vue')), 'officephone');
// 办公通知	
const officeNotice = r => require.ensure([], () => r(require('@views/Home/officeNotice/officeNotice.vue')), 'officeNotice');
// 办公通知正文
const officeNoticeDetails =  r => require.ensure([], () => r(require('@views/Home/officeNoticeDetails/officeNoticeDetails.vue')), 'officeNoticeDetails');
// 电话列表
const officephoneList = r => require.ensure([], () => r(require('@views/Home/officephone/OfficephoneList.vue')), 'officephoneList');
// 搜索共同页面
const searchCommon = r => require.ensure([], () => r(require('@views/searchCommon/searchCommon.vue')), 'searchCommon'); 

Vue.use(Router)

const routes = [{
	path: '/',
	redirect: '/login'
},{
	path: '/page',
	name: 'page',
	meta: {
		title: '北京建筑大学'
	},
	component: Page,
	children: [
		{
			path: '',
			hidden: true,
			redirect: '/page/Home'
		},
		{
			path: 'Home',
			name: 'Home',
			meta: {
				title: '我的门户'
			},
			title: '我的门户',
			component: Home,
			redirect: '/page/Home/homePage', // 重定向
			children: [{
				path: 'homePage',
				name: 'homePage',
				meta: {
					title: '首页'
				},
				title: '首页',
				component: homePage
			},{
				path: 'sendnotice',
				name: 'sendnotice',
				meta: {
						title: '发通知'
				},
				title: '发通知',
				component: sendnotice
			},{
				path: 'noticeIndex',
				name: 'notificationlist',
				meta: {
						title: '校内通知'
				},
				title: '校内通知',
				component: noticeIndex,
				redirect: '/page/Home/noticeIndex/notificationlist', // 重定向
				children: [{
					path: 'notificationlist',
					name: 'notificationlist',
					meta: {
							title: '校内通知'
					},
					title: '校内通知',
					component: notificationlist
				},{
					path: 'content',
					name: 'notificationlist',
					meta: {
							title: '正文'
					},
					title: '正文',
					component: content
				}]
			},{
				path: 'feedbacklist',
				name: 'feedbacklist',
				meta: {
						title: '我的反馈'
				},
				title: '我的反馈',
				component: feedbacklist
			},
			{
				path: 'sendfeedback',
				name: 'sendfeedback',
				meta: {
						title: '我要反馈'
				},
				title: '我要反馈',
				hidden: true,
				component: sendfeedback
			},
			{
				path: 'feedbackcontent',
				name: 'feedbackcontent',
				meta: {
						title: '反馈内容'
				},
				title: '反馈内容',
				hidden: true,
				component: feedbackcontent
			},
			{
				path: 'officeNotice',
				name: 'officeNotice',
				meta: {
					title: '办公通知',
				},
				title: '办公通知',
				hidden: true,
				component: officeNotice
			},
			{
				path: 'officeNoticeDetails',
				name: 'officeNoticeDetails',
				meta: {
					title: '正文',
				},
				title: '正文',
				hidden: true,
				component: officeNoticeDetails
			},
			{
				path: 'officephone',
				name: 'officephone',
				meta: {
						title: '办公电话'
				},
				title: '办公电话',
				component: officephone
			},
			{
				path: 'officephoneList',
				name: 'officephoneList',
				meta: {
						title: '电话列表'
				},
				title: '电话列表',
				hidden: true,
				component: officephoneList
			},{
				path: 'schoolCalendar',
				name: 'schoolCalendar',
				meta: {
					title: '校历日历'
				},
				title: '校历日历',
				component: schoolCalendar
			},{
				path: 'myCollection',
				name: 'myCollection',
				meta: {
					title: '我的收藏'
				},
				title: '我的收藏',
				component: myCollection
			},{
				path: 'sysTemApplication',
				name: 'sysTemApplication',
				meta: {
					title: '系统应用'
				},
				title: '系统应用',
				hidden: true,
				component: sysTemApplication
			},{
				path: 'xiaoNeiNet',
				name: 'xiaoNeiNet',
				meta: {
					title: '校内网站'
				},
				title: '校内网站',
				component: xiaoNeiNet
			},{
				path: 'searchCommon',
				name: 'searchCommon',
				meta: {
					title: '搜索'
				},
				title: '搜索',
				hidden: true,
				component: searchCommon
			}]
		},
		{
			path: 'serviceCenter',
			name: 'serviceCenter',
			meta: {
				title: '服务大厅'
			},
			title: '服务大厅',
			component: serviceCenter
		},
	]
},{
	path: '/login',
	name: 'login',
	meta: {
		title: '统一登录'
	},
	title: '统一登录',
	component: Login,
}]

const router = new Router({
	routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
	// 设置标题
	console.log(to)
	setTitle(to.meta.title);
	// store.commit('SET_UPDATE_TITLE', to.meta.title)
	store.commit('SET_LOADING_STATUS', true);
	!to.params.isCollect && store.commit('SET_HEADER_TITLE', to);  // 存储路由地址及参数名
	if (to.matched.some(r => r.meta.requireAuth)) {
		// let token = window.localStorage.getItem('token')
		// if (token) {
		//     next()
		// } else {
		// next({
		// path: `${baseUrl}/weixinLogin?`,
		// query: { redirect: to.fullPath }
		// })
		// }
	} else {
		next()
	}
})

router.afterEach(function (to) {
	setTimeout(() => {
		store.commit('SET_LOADING_STATUS', false)
	}, 800)
})

export default router
